/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Mariage.java
 *
 * Created on 6 oct. 2011, 11:41:38
 */
package Vue;

import Bdd.MariageRepository;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ListDataListener;

/**
 *
 * @author p0903295
 */
public class Mariage extends javax.swing.JDialog {

    private static final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    /** Creates new form Mariage */
    public Mariage(java.awt.Frame parent, boolean modal) {
        // Initialisation de la vue
        super(parent, modal);
        initComponents();

        // Insertion des numeros pour la date
        int yearNow = 2011;
        for (int i = 1; i <= 31; i++) {
            comboBoxDay.addItem(String.valueOf(i));
        }
        for (int i = 0; i < 12; i++) {
            comboBoxMonth.addItem(months[i]);
        }
        for (int i = yearNow; i >= 1800; i--) {
            comboBoxYear.addItem(String.valueOf(i));
        }

        // Recupération des Vip celibataires
        Bdd.VipRepository vipRepo = new Bdd.VipRepository();
        ArrayList<Metier.Vip> allCelib = vipRepo.loadAllCelib();
        // Séparation des données selon le sexe
        ArrayList<Metier.Vip> celibMen = new ArrayList<Metier.Vip>();
        ArrayList<Metier.Vip> celibWomen = new ArrayList<Metier.Vip>();
        for (Metier.Vip vip : allCelib) {
            if (vip.getSexe()) {
                celibMen.add(vip);
            } else {
                celibWomen.add(vip);
            }
        }
        // Insertion des données dans les comboBox à l'aide de modeles
        comboBoxMenModel = new DefaultComboBoxModel(celibMen.toArray());
        comboBoxMen.setModel(comboBoxMenModel);
        comboBoxWomenModel = new DefaultComboBoxModel(celibWomen.toArray());
        comboBoxWomen.setModel(comboBoxWomenModel);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Ltitle = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        comboBoxWomen = new javax.swing.JComboBox();
        comboBoxMen = new javax.swing.JComboBox();
        titleWhere = new javax.swing.JLabel();
        textFieldLocation = new javax.swing.JTextField();
        titleBetween = new javax.swing.JLabel();
        titleAnd = new javax.swing.JLabel();
        comboBoxDay = new javax.swing.JComboBox();
        titleWhen = new javax.swing.JLabel();
        comboBoxMonth = new javax.swing.JComboBox();
        comboBoxYear = new javax.swing.JComboBox();
        btnProceed = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        labelError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        Ltitle.setFont(new java.awt.Font("Aston-F1", 0, 24));
        Ltitle.setText("proceed in a mariage");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Join peoples", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(153, 153, 153))); // NOI18N

        titleWhere.setText("Where ?");

        titleBetween.setText("Between");

        titleAnd.setText("and");

        titleWhen.setText("When ?");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleWhere)
                    .addComponent(titleAnd)
                    .addComponent(titleBetween)
                    .addComponent(titleWhen))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(comboBoxDay, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBoxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBoxYear, 0, 134, Short.MAX_VALUE))
                    .addComponent(textFieldLocation, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                    .addComponent(comboBoxWomen, 0, 296, Short.MAX_VALUE)
                    .addComponent(comboBoxMen, 0, 296, Short.MAX_VALUE))
                .addGap(24, 24, 24))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleBetween)
                    .addComponent(comboBoxWomen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleAnd)
                    .addComponent(comboBoxMen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleWhere)
                    .addComponent(textFieldLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleWhen)
                    .addComponent(comboBoxDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        btnProceed.setText("Proceed");
        btnProceed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProceedActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        labelError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelError.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(14, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelError, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnProceed, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(Ltitle)
                        .addGap(85, 85, 85))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnProceed});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Ltitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProceed)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnProceedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProceedActionPerformed
        labelError.setText("  ");

        // Validation de la date //
        if (!isValid(Integer.valueOf((String) comboBoxDay.getSelectedItem()), (comboBoxMonth.getSelectedIndex() + 1), Integer.valueOf((String) comboBoxYear.getSelectedItem()))) {
            labelError.setText("Please enter a valid date... (" + (String) comboBoxDay.getSelectedItem() + "-" + (comboBoxMonth.getSelectedIndex() + 1) + "-" + (String) comboBoxYear.getSelectedItem() + ")");
            return;
        }
        System.out.println("Création date : " + Integer.valueOf((String) comboBoxYear.getSelectedItem()) + "-" + (comboBoxMonth.getSelectedIndex() + 1) + "-" + Integer.valueOf((String) comboBoxDay.getSelectedItem()));
        Date dateActu = new Date(Integer.valueOf((String) comboBoxYear.getSelectedItem())-1900, (comboBoxMonth.getSelectedIndex()), Integer.valueOf((String) comboBoxDay.getSelectedItem()));
        
        System.out.println("Comparaison date "+((Metier.Vip)comboBoxMenModel.getSelectedItem()).getDate_naiss()+" et "+dateActu);
        System.out.println("Comparaison date "+((Metier.Vip)comboBoxWomenModel.getSelectedItem()).getDate_naiss()+" et "+dateActu);
        if ((((Metier.Vip) comboBoxMenModel.getSelectedItem()).getDate_naiss().compareTo(dateActu)) > 0 || (((Metier.Vip) comboBoxWomenModel.getSelectedItem()).getDate_naiss().compareTo(dateActu) > 0)) {
            labelError.setText("Date impossible, look at birth dates...");
            return;
        }
        
        // Validation du lieu //
        if (textFieldLocation.getText().isEmpty()) {
            labelError.setText("Please enter a valid location...");
            return;
        }

        
        Metier.Mariage mariageActu = new Metier.Mariage((Metier.Vip) comboBoxMenModel.getSelectedItem(), (Metier.Vip) comboBoxWomenModel.getSelectedItem(), dateActu, textFieldLocation.getText());
        Bdd.MariageRepository mariageRepo = new Bdd.MariageRepository();
        int resultInsertion = mariageRepo.insertObject(mariageActu);
        switch (resultInsertion) {
            case MariageRepository.INSERT_OK:
                this.dispose();
                return;
            case MariageRepository.INSERT_ERROR_EXIST:
                labelError.setText("This aleady exist !");
                return;
            case MariageRepository.INSERT_ERROR_VIP:
                labelError.setText("Oups, Vip selected are deleted...");
                return;
            case MariageRepository.INSERT_ERROR_NET:
                labelError.setText("Connection error...");
                return;
            default:
                labelError.setText("Unknow error...");
                return;
        }
}//GEN-LAST:event_btnProceedActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    public boolean isValid(int jour, int mois, int annee) {
        Calendar c = Calendar.getInstance();
        c.setLenient(false);
        c.set(annee, mois, jour);
        try {
            c.getTime();
        } catch (IllegalArgumentException iAE) {
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Ltitle;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnProceed;
    private javax.swing.JComboBox comboBoxDay;
    private javax.swing.JComboBox comboBoxMen;
    private javax.swing.JComboBox comboBoxMonth;
    private javax.swing.JComboBox comboBoxWomen;
    private javax.swing.JComboBox comboBoxYear;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelError;
    private javax.swing.JTextField textFieldLocation;
    private javax.swing.JLabel titleAnd;
    private javax.swing.JLabel titleBetween;
    private javax.swing.JLabel titleWhen;
    private javax.swing.JLabel titleWhere;
    // End of variables declaration//GEN-END:variables
    DefaultComboBoxModel comboBoxMenModel, comboBoxWomenModel;
}
